﻿@page "/project/add"

@inject IJSRuntime JsRuntime
@inject NavigationManager NavigationManager
@inject IProjectService ProjectService
@inject IUserService UserService

<PageTitle>新增项目</PageTitle>

<EditForm Model="_projectDto" OnSubmit="Save">
    <div class="input-group mb-2">
        <span class="input-group-text">主绘图人</span>
        <select class="form-select" @bind="@_projectDto.DesignerId">
            @foreach (var user in _userDtos)
            {
                @if (_projectDto != null && user.Id == _projectDto.DesignerId)
                {
                    <option selected="selected" value="@user.Id">@user.UserName</option>
                }
                else
                {
                    <option value="@user.Id">@user.UserName</option>
                }
            }
        </select>
    </div>
    <div class="input-group mb-2 mt-2">
        <span class="input-group-text">项目编号</span>
        <input class="form-control" placeholder="ODP" @bind-value="@_projectDto.OdpNumber" />
    </div>
    <div class="input-group mb-2">
        <span class="input-group-text">项目名称</span>
        <input class="form-control" placeholder="Name" @bind-value="@_projectDto.Name" />
    </div>
    <div class="input-group mb-2" style="width: 230px">
        <span class="input-group-text">交货日期</span>
        <input type="date" class="form-control" @bind-value="@_projectDto.DeliveryDate" />
    </div>
    <div class="input-group mb-2">
        <span class="input-group-text">项目类型</span>
        <select class="form-select" @bind="@_projectDto.ProjectType">
            <CompEnumList Type="typeof(ProjectType_e)"/>
        </select>
    </div>
    <div class="input-group mb-2">
        <span class="input-group-text">风险等级</span>
        <select class="form-select" @bind="@_projectDto.RiskLevel">
            <CompEnumList Type="typeof(RiskLevel_e)"/>
        </select>
        @*Todo:风险值=概率*影响，概率影响矩阵*@
        <button class="btn btn-warning" type="button">风险评级</button>
    </div>
    <div class="input-group mb-2">
        <span class="input-group-text">特殊要求</span>
        <textarea class="form-control" placeholder="Special Notes，可输入多行" style="height: 350px" @bind="@_projectDto.SpecialNotes"></textarea>
    </div>

    <CompSaveCancelButtons CancelUrl="projects"/>
</EditForm>

@code {
    //初始化发货日期为今天后的2周
    private ProjectDto _projectDto = new (){DeliveryDate = DateTime.Now.AddDays(14)};
    //todo:绑定MainPlan
    private List<UserDto> _userDtos = new();
    protected override async Task OnInitializedAsync()
    {
        var uResponse = await UserService.GetUsersInRolesAsync("pm,dsr");
        if (uResponse.Status) _userDtos = uResponse.Result;
    }

    private async Task Save()
    {
        if (string.IsNullOrWhiteSpace(_projectDto.OdpNumber) || string.IsNullOrWhiteSpace(_projectDto.Name))
        {
            await JsRuntime.InvokeVoidAsync("alert", "填写的信息不完整");
            return;
        }
        //为了避免FSO号前后有空格，导致后续创建文件夹时出现错误，此处消除空格
        _projectDto.OdpNumber = _projectDto.OdpNumber.Trim().ToUpper();

        var same = await ProjectService.GetFirstOrDefaultByOdpNumberAsync(_projectDto.OdpNumber);
        if (same.Status)
        {
            await JsRuntime.InvokeVoidAsync("alert", "项目编号重复，请重新修改项目编号！");
            return;
        }
        var result = await ProjectService.AddAsync(_projectDto);
        //跳转回主页
        if (result.IsSuccessStatusCode) NavigationManager.NavigateTo("projects");
    }
}
